package com.company.bkst.dao;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Date;

import com.company.bkst.Order;

public class OrderDAO {

	private Order order;

	public OrderDAO(){}
	
	public OrderDAO(Order order) {
		this.order = order;
	}

	public Order getOrderByID(String orderID) {
		return order;
	}

	public ArrayList<Order> getAllOrders() {
		
		return null;
	}
	
	public Boolean saveOrder(Order order){
		try {
			int new_id = 0;
			File file = new File("D:\\WorkspaceEclipse\\Projects\\BkStoreServices\\orders.txt");
 
			// if file doesnt exists, then create it
			if (!file.exists()) {
				file.createNewFile();
				new_id = new_id + 1;
			}
			else{
				String linha;
				FileReader fr = new FileReader(file);
				BufferedReader br = new BufferedReader(fr);
				while((linha = br.readLine()) != null){
					new_id = new_id + 1;
				}
				
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile(), true);
			BufferedWriter bw = new BufferedWriter(fw);
			bw.write(new_id + " " + order.getBook().getBookId() + " " + order.getQuantity() + " " + new Date());
			bw.newLine();
			bw.close();
 
		} catch (IOException e) {
			return false;
		}
		return true;
	}
	
	
	public Boolean cancelOrder(String orderID){
		
		return true;
	}
		
}
